<?php namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Item;
use App\Product;
use App\Type;
use App\Picture;
use DB;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\Request;

class ProductController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share('pageName','product');
    }

    public function getIndex(Request $request)
    {
        $appends = $request->all();
        $params = array();
        $class = array();
        $type = isset($appends["ck"]) ? $appends["ck"] : 0;
        $ptype = isset($appends["pk"]) ? $appends["pk"] : 0;
        $types = parent::getAllTypes();
        $products = Product::orderBy('RCMD','DESC')->orderBy('hot','DESC');

        if(isset($ptype) && 0 != $ptype){
            $params["pk"] = $ptype;
            $class["pk"] = $types[$ptype]['name'];
            $products = $products->where("parent_id", $ptype);
        }
        if(isset($type) && 0 != $type){
            $params["ck"] = $type;
            foreach($types[$ptype]["data"] as $key=>$item){
                if($key == $type){
                    $class['ck'] = $item;
                }
            }
            $products = $products->where('type_id',$type);
        }

        if(isset($appends['name']) && "" != $appends['name']){
            $params["name"] = $appends['name'];
            $products = $products->where('name','like','%'.$appends['name'].'%');
        }

        $products = $products->paginate(config('site.pageSize'));

        return view('site.product.index',[
            'types' => $types,
            'typeShow' => $type,
            'appends' => $appends,
            'params' => $params,
            'class' => $class,
            'products' => $products,
        ]);
    }

    public function getDetail($id = 0)
    {
        $types = parent::getAllTypes();
        $product = Product::find($id);
        $rproducts = array();
        if($product->recommend && strlen($product->recommend)>0){
            $rproducts = DB::select('select * from products where id in ('.$product->recommend.')');
        }
        $pics = DB::select('select * from pictures where id in('. $product->pic .')');
        $i=0;
        foreach ($pics as $key=>$item){
            $item->imgUrl = 'upload'.DS.$item->dir.DS.$item->sub_dir.DS.$item->pic;
        }
        $class = array();
        $class['pk'] = $types[$product->parent_id]['name'];
        foreach($types[$product->parent_id]["data"] as $key=>$item){
            if($key == $product->type_id){
                $class['ck'] = $item;
            }
        }
        //print_r($class);
        //exit();
        return view('site.product.detail',[
            'class'         => $class,
            'types'         => $types,
            'typeShow'      => $product->type_id,
            'product'       => $product,
            'pics'          => $pics,
            'recommends'    => $rproducts
        ]);
    }

    //通用方法  public function
    private function getParents()
    {
        if (Cache::has('parents')) {
            $parents = Cache::get('parents');
        } else {
            if('zh_cn' != (parent::setLang())){
                $parents = Type::where('parent_id', 0)->lists('name', 'id');
            } else {
                $parents = Type::where('parent_id', 0)->lists('name_cn', 'id');
            }
            Cache::forever('parents', $parents);
        }
        return $parents;
    }

}